/*
 * Author: Alvin
 * Modified By: Alvin
 * Created Date: 2023-10-08 17:17:37
 * Last Modified: 2023-10-08 18:58:56
 * Description:
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/pages/devices/device_list/device_list_model.dart';
import 'package:yt_devops/components/my_gesture_detector.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/enums.dart';

class SiteDeviceListDeviceWidget extends StatelessWidget {
  final DeviceListModel model;
  final bool isSort;
  final bool unbindingPermission;
  void Function(DeviceListModel, BuildContext)? onItemTaped;
  void Function(DeviceListModel)? sortToTopClick;
  SiteDeviceListDeviceWidget(this.model,this.isSort, {this.onItemTaped,this.sortToTopClick,
    this.unbindingPermission = true,super.key});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        child: Container(
          padding: EdgeInsets.fromLTRB(16.w, 0, 16.w, 0),
          margin:  EdgeInsets.only(top: 10,right: 16.w,left: 16.w),

          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
            color: Colour.white,
          ),
          child: Column(
            children: [
              SizedBox(
                height: 53,
                child: Row(
                  children: [
                    Expanded(
                        child: Text(
                      model.name ?? '',
                      style: TextStyle(
                          color: Colour.mainTextColor,
                          fontSize: 18.sp,
                          fontWeight: FontWeight.w500),
                      overflow: TextOverflow.ellipsis,
                    )),
                    isSort?sortWidget():Padding(
                      padding: EdgeInsets.only(left: 10.w),
                      child: statusWidget(model.canUnbind??0,model.connectStatus!,context),
                    )
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(bottom: 8),
                child: Row(
                  children: [
                    SizedBox(
                      width: 75.w,
                      child: Text(ResKey.siteAccessDeviceSnNumber.tr, style: TextStyles.lf14),
                    ),
                    Expanded(
                      child: Text(
                        model.sn ?? '',
                        style: TextStyle(
                          color: Colour.mainTextColor,
                          fontSize: 14.sp,
                        ),
                        overflow: TextOverflow.ellipsis,
                      ),
                    )
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(bottom: 8),
                child: Row(
                  children: [
                    SizedBox(
                      width: 75.w,
                      child: Text(ResKey.siteAccessDeviceDeviceType.tr, style: TextStyles.lf14),
                    ),
                    Expanded(
                      child: Text(
                        model.productTypeName ?? '',
                        style: TextStyle(
                          color: Colour.mainTextColor,
                          fontSize: 14.sp,
                        ),
                        overflow: TextOverflow.ellipsis,
                      ),
                    )
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(bottom: 8),
                child: Row(
                  children: [
                    SizedBox(
                      width: 75.w,
                      child: Text(ResKey.siteAccessDeviceDeviceModel.tr, style: TextStyles.lf14),
                    ),
                    Expanded(
                      child: Text(
                        model.model ?? '',
                        style: TextStyle(
                          color: Colour.mainTextColor,
                          fontSize: 14.sp,
                        ),
                        overflow: TextOverflow.ellipsis,
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
        onTap: () {
          if (onItemTaped != null) {
            // onItemTaped!(model,context);
          }
        });
  }

  //该设备能否解绑（0不能解绑 1能解绑)
  Widget statusWidget(int isVisible, ConnectStatusType type,BuildContext context) {
    return Visibility(
      // visible: isVisible == 1 && type != ConnectStatusType.unConfig && unbindingPermission,
        visible:unbindingPermission,
        child: GestureDetector(
      onTap: () {
        if (onItemTaped != null) {
          onItemTaped!(model, context);
        }
      },
      child: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 4.w),
        height: 20.h,
        child: Image(
          image: const AssetImage(ImageAssets.iconUnbindingDevice),
          height: 20.w,
          width: 20.w,
        ),
      ),
    ));
  }
  Widget sortWidget(){
    return Container(
      width: 80.w,
      color: Colors.white,
      alignment: Alignment.centerRight,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Spacer(),
          MyGestureDetector(onTap: (){
            if(sortToTopClick!= null){
              sortToTopClick!(model);
            }
            Log.d('置顶');
          }, child: Container(
            padding: EdgeInsets.only(right: 10.w),
            child: Image.asset('assets/images/device_to_top.png'),
          )),
          Container(
            padding: EdgeInsets.only(left:10.w,),
            child: Image.asset('assets/images/device_drag_sort.png'),
          ),
        ],
      ),
    );
  }

}
